package view;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import java.awt.Color;
import java.io.IOException;
import java.lang.reflect.Array;

import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JList;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;

import model.HtmlFile;
import model.Word;
import model.WordPair;


public class TestJFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * @uml.property  name="contentPane"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel("com.jgoodies.looks.windows.WindowsLookAndFeel");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestJFrame frame = new TestJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	

	/**
	 * Create the frame.
	 */
	public TestJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 683, 375);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 32, 125, 269);
		
		JLabel lblWordCount = new JLabel("Word Count:");
		lblWordCount.setBounds(144, 32, 67, 15);
		
		final JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(217, 32, 30, 15);
		
		JLabel lblSentenceCount = new JLabel("Sentence Count:");
		lblSentenceCount.setBounds(144, 51, 85, 15);
		
		final JLabel lblSentenceCnt = new JLabel("");
		lblSentenceCnt.setBounds(235, 51, 28, 13);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(266, 33, 121, 237);
		DefaultMutableTreeNode top =
            new DefaultMutableTreeNode("Html Files");
        createNodes(top);
        
        final JTree tree = new JTree(top);
		
		final JList list = new JList();
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
			}
		});
		
		scrollPane_1.setViewportView(list);
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(475, 32, 140, 237);
		
		final JLabel lblError = new JLabel("");
		lblError.setForeground(new Color(204, 0, 0));
		lblError.setBackground(new Color(204, 0, 0));
		lblError.setBounds(266, 12, 121, 11);
		
		final JList list_1 = new JList();
		scrollPane_2.setViewportView(list_1);
		
		JLabel lblWordList = new JLabel("Word List");
		scrollPane_1.setColumnHeaderView(lblWordList);
		
		JButton btnFindPair = new JButton("Find Pair");
		btnFindPair.setBounds(397, 107, 57, 24);
		btnFindPair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 lblError.setText("");
				if(list.getSelectedValues().length != 2){
				    lblError.setText("Select only 2 words");
					list_1.removeAll();
					list_1.repaint();
				}
				else{
					Object [] obj=list.getSelectedValues();
					Word word1=(Word) Array.get(obj, 0);
					Word word2=(Word) Array.get(obj, 1);
					DefaultMutableTreeNode node = (DefaultMutableTreeNode)
	                tree.getLastSelectedPathComponent();
	        		System.out.println(word1.getWord()+word2.getWord());
	        		 if (node == null) return;
	        		 Object nodeInfo = node.getUserObject();
	                 HtmlFile x=(HtmlFile) nodeInfo;
	                 WordPair p= new WordPair(word1.getWord(),word2.getWord());
	                
	                 list_1.setListData( x.findPairOccurences(p).toArray());
					
				}              
			}
		});
		
		JLabel label = new JLabel("Sentence#->Occurence");
		label.setBounds(475, 276, 151, 25);
		
		JLabel lblWordPairOccurences = new JLabel("Word Pair Occurences");
		scrollPane_2.setColumnHeaderView(lblWordPairOccurences);
		
		
		
	
		
	
		
		
		
		
        tree.addTreeSelectionListener(new TreeSelectionListener() {
        	public void valueChanged(TreeSelectionEvent e) {
        		DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                tree.getLastSelectedPathComponent();
        		
        		 if (node == null) return;
        		 Object nodeInfo = node.getUserObject();
                 HtmlFile x=(HtmlFile) nodeInfo; 
                 if(!x.isProcessed()){
	                 //events when html file clicked
	                 x.extractText();
	                
	                 try {
						x.wordCollector();
					 } catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					 }
					 x.setProcessed(true);
                 }
                 lblSentenceCnt.setText(String.valueOf(x.getSentenceCount()));
				 lblNewLabel.setText(String.valueOf(x.getWordCount()));
				 list.setListData(createList(x));
                 
        	}
        });
        tree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);
        contentPane.setLayout(null);
        
   
        tree.setForeground(Color.GRAY);
		tree.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		
		scrollPane.setViewportView(tree);
		contentPane.add(scrollPane);
		contentPane.add(lblSentenceCount);
		contentPane.add(lblSentenceCnt);
		contentPane.add(lblWordCount);
		contentPane.add(lblNewLabel);
		contentPane.add(lblError);
		contentPane.add(scrollPane_1);
		contentPane.add(btnFindPair);
		contentPane.add(scrollPane_2);
		contentPane.add(label);
	}
    

	private Object[] createList(HtmlFile file) {
		return file.getWordMap().values().toArray();
		
		
	}


	private void createNodes(DefaultMutableTreeNode top) {
		// TODO Auto-generated method stub
		DefaultMutableTreeNode file = null;
		file = new DefaultMutableTreeNode(new HtmlFile("arnold.html"));
        top.add(file);
        file = new DefaultMutableTreeNode(new HtmlFile("tutorial.html"));
        top.add(file);
	}
}
